{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "8b0bbaff",
   "metadata": {},
   "source": [
    "# 本周内容\n",
    "## 知识概念\n",
    "> 1. 一切都是I/O：软工到产品的一般化知识\n",
    "> 1. 语音识别:speech recognition\n",
    "> 1. 语音唤醒\n",
    "> 1. 自动语音识别:Automatic Speech Recognition, 简称<font color=red> ASR </font>\n",
    ">> 1. 其目标是以电脑自动将人类的语音内容转换为相应的文字。与说话人识别及说话人确认不同，后者尝试识别或确认发出语音的说话人而非其中所包含的词汇内容。\n",
    "> 1. 语音合成:text to speech,简称<font color=red> TTS </font>\n",
    ">> 1. 将文字转化为语音的一种技术，类似于人类的嘴巴，通过不同的音色说出想表达的内容。 在语音合成技术中，主要分为 语言分析部分 和 声学系统部分 ，也称为 前端部分 和 后端部分， 语言分析部分主要是根据输入的文字信息进行分析，生成对应的语言学规格书，想好该怎么读；声学系统部分主要是根据语音分析部分提供的语音学规格书，生成对应的音频，实现发声的功能。\n",
    "\n",
    "# 语音识别测试"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "353a8fc3",
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "import json\n",
    "import time\n",
    "\n",
    "IS_PY3 = sys.version_info.major == 3\n",
    "\n",
    "from urllib.request import urlopen\n",
    "from urllib.request import Request\n",
    "from urllib.error import URLError\n",
    "from urllib.parse import urlencode\n",
    "timer = time.perf_counter\n",
    "\n",
    "\n",
    "API_KEY = 'k1CcDGfxcKeNrsufq5HVCbGb'\n",
    "SECRET_KEY = 'Vg7GVNkZr6jOrm89KxGbgH5GPPkAldMs'\n",
    "\n",
    "# 需要识别的文件\n",
    "AUDIO_FILE = './audio/16k.m4a'  # 只支持 pcm/wav/amr 格式，极速版额外支持m4a 格式\n",
    "# 文件格式\n",
    "FORMAT = AUDIO_FILE[-3:];  # 文件后缀只支持 pcm/wav/amr 格式，极速版额外支持m4a 格式\n",
    "\n",
    "CUID = '123456PYTHON';\n",
    "# 采样率\n",
    "RATE = 16000;  # 固定值\n",
    "\n",
    "# 普通版\n",
    "\n",
    "DEV_PID = 1537;  # 1537 表示识别普通话，使用输入法模型。根据文档填写PID，选择语言及识别模型\n",
    "ASR_URL = 'http://vop.baidu.com/server_api'\n",
    "SCOPE = 'audio_voice_assistant_get'  # 有此scope表示有asr能力，没有请在网页里勾选，非常旧的应用可能没有\n",
    "\n",
    "\n",
    "\"\"\"  TOKEN start \"\"\"\n",
    "TOKEN_URL = 'http://aip.baidubce.com/oauth/2.0/token'\n",
    "# 获取access_token\n",
    "def fetch_token():\n",
    "    params = {'grant_type': 'client_credentials',\n",
    "              'client_id': API_KEY,\n",
    "              'client_secret': SECRET_KEY}\n",
    "    post_data = urlencode(params)\n",
    "    if (IS_PY3):\n",
    "        post_data = post_data.encode('utf-8')\n",
    "    req = Request(TOKEN_URL, post_data)\n",
    "    try:\n",
    "        f = urlopen(req)\n",
    "        result_str = f.read()\n",
    "    except URLError as err:\n",
    "        print('token http response http code : ' + str(err.code))\n",
    "        result_str = err.read()\n",
    "    if (IS_PY3):\n",
    "        result_str = result_str.decode()\n",
    "\n",
    "    print(result_str)\n",
    "    result = json.loads(result_str)\n",
    "    print(result)\n",
    "    if ('access_token' in result.keys() and 'scope' in result.keys()):\n",
    "        if SCOPE and (not SCOPE in result['scope'].split(' ')):  # SCOPE = False 忽略检查\n",
    "            raise DemoError('scope is not correct')\n",
    "        return result['access_token']\n",
    "    else:\n",
    "        raise DemoError('MAYBE API_KEY or SECRET_KEY not correct: access_token or scope not found in token response')\n",
    "\n",
    "\n",
    "\"\"\"  TOKEN end \"\"\"\n",
    "\n",
    "# 语音识别接口的调用\n",
    "\n",
    "token = fetch_token()\n",
    "speech_data = []\n",
    "# 打开目标语音文件\n",
    "with open(AUDIO_FILE, 'rb') as speech_file:\n",
    "    speech_data = speech_file.read()\n",
    "length = len(speech_data)\n",
    "if length == 0:\n",
    "    raise DemoError('file %s length read 0 bytes' % AUDIO_FILE)\n",
    "\n",
    "params = {\n",
    "    'cuid': CUID, \n",
    "    'token': token, \n",
    "    'dev_pid': DEV_PID\n",
    "}\n",
    "#测试自训练平台需要打开以下信息\n",
    "#params = {'cuid': CUID, 'token': token, 'dev_pid': DEV_PID, 'lm_id' : LM_ID}\n",
    "params_query = urlencode(params);\n",
    "\n",
    "headers = {\n",
    "    'Content-Type': 'audio/' + FORMAT + '; rate=' + str(RATE),\n",
    "    'Content-Length': length\n",
    "}\n",
    "\n",
    "url = ASR_URL + \"?\" + params_query\n",
    "\n",
    "# print post_data\n",
    "req = Request(ASR_URL + \"?\" + params_query, speech_data, headers)\n",
    "# 异常处理，避免出现红色的bug（提前预判可能出现的异常）\n",
    "try:\n",
    "    begin = timer()\n",
    "    f = urlopen(req)\n",
    "    result_str = f.read()\n",
    "    print(\"Request time cost %f\" % (timer() - begin))\n",
    "except  URLError as err:\n",
    "    print('asr http response http code : ' + str(err.code))\n",
    "    result_str = err.read()\n",
    "\n",
    "if (IS_PY3):\n",
    "    result_str = str(result_str, 'utf-8')\n",
    "print(result_str)\n",
    "with open(\"result.txt\", \"w\") as of:\n",
    "    of.write(result_str)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "615eb9f7",
   "metadata": {},
   "source": [
    "# 真对话机器人（文本回复）\n",
    "* 1. 识别语音-->转换百度语音识别指定格式文件\n",
    "* 2. 调用百度asr接口，转换成文本\n",
    "* 3. 回复？数据结构：字典 {key:value} , key（人） 作为输入 Input, value（机器） 作为 Output\n",
    "> 1. {\\\"你好呀\\\":\\\"你也好呀，看起来心情不错呀！\\\"}\n",
    "* 4. 回复value的信息\n",
    "* 5. 接语音合成，完成智能语音对话机器人\n",
    "* 思考：问天气（调用天气API），问位置（调用高德API），问周边（调用高德周边搜索POI...）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1b9ff6ec",
   "metadata": {},
   "outputs": [],
   "source": [
    "qa = {\n",
    "    \"你好呀\":\"你也好呀，看起来心情不错呀！\",\n",
    "    \"你叫什么\":\"我是集美貌与才华一身的小度小度呀\",\n",
    "    \"设置闹钟\":\"您想要设置几点的闹钟呢？现在已经晚上12点了，早点休息呀\"\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9306afa1",
   "metadata": {},
   "outputs": [],
   "source": [
    "qa.get(\"你好呀\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8da4b845",
   "metadata": {},
   "outputs": [],
   "source": [
    "qa.get(\"你叫什么\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eb1f9476",
   "metadata": {},
   "source": [
    "> 1. 调用电脑麦克风，将语音转换音频文件\n",
    "> 1. 音频文件---> 调用百度ASR 接口"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "31e8527a",
   "metadata": {},
   "outputs": [],
   "source": [
    "API_KEY = 'k1CcDGfxcKeNrsufq5HVCbGb'\n",
    "SECRET_KEY = 'Vg7GVNkZr6jOrm89KxGbgH5GPPkAldMs'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a6cca9a8",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 安装 speech_recognition\n",
    "pip install SpeechRecognition"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c214b95a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 安装 PyAudio\n",
    "Not found name \"PyAudio\" moudle"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b7b69dae",
   "metadata": {},
   "outputs": [],
   "source": [
    "pip install pipwin"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bf6437bc",
   "metadata": {},
   "outputs": [],
   "source": [
    "pipwin install PyAudio"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "504be8f4",
   "metadata": {},
   "outputs": [],
   "source": [
    "import speech_recognition\n",
    "\n",
    "r = speech_recognition.Recognizer()\n",
    "\n",
    "with speech_recognition.Microphone() as source:\n",
    "    audio = r.listen(source)\n",
    "# 将数据保存到wav文件中\n",
    "with open(\"1.wav\", \"wb\") as f: \n",
    "    f.write(audio.get_wav_data(convert_rate=16000))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1d9fab0a",
   "metadata": {},
   "outputs": [],
   "source": [
    "import speech_recognition\n",
    "\n",
    "r = speech_recognition.Recognizer()\n",
    "with speech_recognition.Microphone() as source:\n",
    "    audio = r.listen(source)\n",
    "# 将数据保存到wav文件中\n",
    "with open(\"2.wav\", \"wb\") as f: \n",
    "    f.write(audio.get_wav_data(convert_rate=16000))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1f998b46",
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "import json\n",
    "import time\n",
    "from token import fetch_token\n",
    "\n",
    "IS_PY3 = sys.version_info.major == 3\n",
    "\n",
    "from urllib.request import urlopen\n",
    "from urllib.request import Request\n",
    "from urllib.error import URLError\n",
    "from urllib.parse import urlencode\n",
    "timer = time.perf_counter\n",
    "\n",
    "# 需要识别的文件\n",
    "AUDIO_FILE = '2.wav'  # 只支持 pcm/wav/amr 格式，极速版额外支持m4a 格式\n",
    "# 文件格式\n",
    "FORMAT = AUDIO_FILE[-3:];  # 文件后缀只支持 pcm/wav/amr 格式，极速版额外支持m4a 格式\n",
    "\n",
    "CUID = '123456PYTHON';\n",
    "# 采样率\n",
    "RATE = 16000;  # 固定值\n",
    "\n",
    "# 普通版\n",
    "\n",
    "DEV_PID = 1537;  # 1537 表示识别普通话，使用输入法模型。根据文档填写PID，选择语言及识别模型\n",
    "ASR_URL = 'http://vop.baidu.com/server_api'\n",
    "SCOPE = 'audio_voice_assistant_get'  # 有此scope表示有asr能力，没有请在网页里勾选，非常旧的应用可能没有\n",
    "\n",
    "\n",
    "# 语音识别接口的调用\n",
    "if __name__ == '__main__':\n",
    "    token = fetch_token()\n",
    "    speech_data = []\n",
    "    # 打开目标语音文件\n",
    "    with open(AUDIO_FILE, 'rb') as speech_file:\n",
    "        speech_data = speech_file.read()\n",
    "    length = len(speech_data)\n",
    "    if length == 0:\n",
    "        raise DemoError('file %s length read 0 bytes' % AUDIO_FILE)\n",
    "\n",
    "    params = {\n",
    "        'cuid': CUID, \n",
    "        'token': token, \n",
    "        'dev_pid': DEV_PID\n",
    "    }\n",
    "    #测试自训练平台需要打开以下信息\n",
    "    #params = {'cuid': CUID, 'token': token, 'dev_pid': DEV_PID, 'lm_id' : LM_ID}\n",
    "    params_query = urlencode(params);\n",
    "\n",
    "    headers = {\n",
    "        'Content-Type': 'audio/' + FORMAT + '; rate=' + str(RATE),\n",
    "        'Content-Length': length\n",
    "    }\n",
    "\n",
    "    url = ASR_URL + \"?\" + params_query\n",
    "   \n",
    "    # print post_data\n",
    "    req = Request(ASR_URL + \"?\" + params_query, speech_data, headers)\n",
    "    # 异常处理，避免出现红色的bug（提前预判可能出现的异常）\n",
    "    try:\n",
    "        begin = timer()\n",
    "        f = urlopen(req)\n",
    "        result_str = f.read()\n",
    "#         print(\"Request time cost %f\" % (timer() - begin))\n",
    "    except  URLError as err:\n",
    "        print('asr http response http code : ' + str(err.code))\n",
    "        result_str = err.read()\n",
    "\n",
    "    if (IS_PY3):\n",
    "        result_str = str(result_str, 'utf-8')  \n",
    "#    print(eval(result_str)[\"result\"][0][:-1]) # eval:str(字典) ---> dict\n",
    "    print(qa.get(eval(result_str)[\"result\"][0][:-1]))\n",
    "    with open(\"result.txt\", \"w\") as of:\n",
    "        of.write(result_str)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "efd19f5d",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "41d57c06",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "da051880",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c70649ea",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b6da2cb2",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "196bcbf8",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
